<template>
  <div class="register-container">
    <div class="register-card">
      <div class="register-header">
        <h1 class="title">中医药文化知识科普系统</h1>
        <div class="sub-title">用户注册</div>
        <div class="divider"></div>
      </div>

      <el-form class="register-form" label-position="top">
        <!-- 基础信息 -->
        <div class="form-section">
          <div class="section-title">基础信息</div>
          <el-form-item label="账号" class="form-item">
            <el-input
              v-model="ruleForm.username"
              placeholder="请输入账号"
              class="custom-input"
            />
          </el-form-item>

          <el-form-item label="密码" class="form-item">
            <el-input
              type="password"
              v-model="ruleForm.password"
              placeholder="请输入密码"
              show-password
              class="custom-input"
            />
          </el-form-item>

          <el-form-item label="确认密码" class="form-item">
            <el-input
              type="password"
              v-model="ruleForm.repetitionPassword"
              placeholder="请再次输入密码"
              show-password
              class="custom-input"
            />
          </el-form-item>
        </div>

        <!-- 用户信息 -->
        <div class="form-section" v-if="tableName == 'yonghu'">
          <div class="section-title">详细信息</div>
          <el-form-item label="用户姓名" class="form-item">
            <el-input
              v-model="ruleForm.yonghuName"
              placeholder="请输入真实姓名"
              class="custom-input"
            />
          </el-form-item>

          <el-form-item label="手机号码" class="form-item">
            <el-input
              v-model="ruleForm.yonghuPhone"
              placeholder="请输入11位手机号"
              class="custom-input"
            />
          </el-form-item>

          <el-form-item label="身份证号" class="form-item">
            <el-input
              v-model="ruleForm.yonghuIdNumber"
              placeholder="请输入18位身份证号码"
              class="custom-input"
            />
          </el-form-item>

          <el-form-item label="电子邮箱" class="form-item">
            <el-input
              v-model="ruleForm.yonghuEmail"
              placeholder="请输入常用邮箱"
              class="custom-input"
            />
          </el-form-item>
        </div>

        <!-- 操作按钮 -->
        <div class="form-actions">
          <el-button type="primary" class="register-btn" @click="login()">
            立即注册
          </el-button>
          <el-button class="cancel-btn" @click="close()"> 返回登录 </el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>


<script>
export default {
  data() {
    return {
      ruleForm: {},
      tableName: "",
      rules: {},
      sexTypesOptions: [],
      yonghuPhotoFlag: false, //用于刷新文件上传
    };
  },
  mounted() {
    let table = this.$storage.get("loginTable");
    this.tableName = table;

    //级联表的下拉框查询
  },
  methods: {
    // 获取uuid
    getUUID() {
      return new Date().getTime();
    },
    yonghuPhotoUploadChange(fileUrls) {
      this.ruleForm.yonghuPhoto = fileUrls;
      if (this.yonghuPhotoFlag) {
        this.yonghuPhotoFlag = false;
      } else {
        this.yonghuPhotoFlag = true;
      }
    },
    close() {
      this.$router.push({ path: "/login" });
    },
    // 注册
    login() {
      if (!this.ruleForm.username) {
        this.$message.error("账号不能为空");
        return;
      }
      if (!this.ruleForm.password) {
        this.$message.error("密码不能为空");
        return;
      }
      if (!this.ruleForm.repetitionPassword) {
        this.$message.error("重复密码不能为空");
        return;
      }
      if (this.ruleForm.repetitionPassword != this.ruleForm.password) {
        this.$message.error("密码和重复密码不一致");
        return;
      }
      if (!this.ruleForm.yonghuName && "yonghu" == this.tableName) {
        this.$message.error("用户姓名不能为空");
        return;
      }
      if (
        "yonghu" == this.tableName &&
        this.ruleForm.yonghuPhone &&
        !this.$validate.isMobile(this.ruleForm.yonghuPhone)
      ) {
        this.$message.error("手机应输入手机格式");
        return;
      }
      if (!this.ruleForm.yonghuIdNumber && "yonghu" == this.tableName) {
        this.$message.error("用户身份证号不能为空");
        return;
      }
      // yonghu 中的 用户头像 判空处理
      // if('yonghu' == this.tableName && (this.ruleForm.yonghuPhoto== null ||this.ruleForm.yonghuPhoto== "")){
      //     this.$message.error("用户头像不能为空");
      //     return
      // }
      if (
        "yonghu" == this.tableName &&
        this.ruleForm.yonghuEmail &&
        !this.$validate.isEmail(this.ruleForm.yonghuEmail)
      ) {
        this.$message.error("邮箱应输入邮件格式");
        return;
      }
      // yonghu 中的 创建时间 判空处理
      // if('yonghu' == this.tableName && (this.ruleForm.createTime== null ||this.ruleForm.createTime== "")){
      //     this.$message.error("创建时间不能为空");
      //     return
      // }
      this.$http({
        url: `${this.tableName}/register`,
        method: "post",
        data: this.ruleForm,
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: "注册成功,请登录后在个人中心页面补充个人数据",
            type: "success",
            duration: 1500,
            onClose: () => {
              this.$router.replace({ path: "/login" });
            },
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.register-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #c1e1c1 0%, #b8d8b8 50%, #a3c9a3 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;

  .register-card {
    background: rgba(255, 255, 255, 0.96);
    width: 580px;
    padding: 40px 50px;
    border-radius: 15px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
    transform: translateY(-5%);

    .register-header {
      text-align: center;
      margin-bottom: 30px;

      .title {
        font-size: 26px;
        color: #2d5d2d;
        font-weight: 600;
        letter-spacing: 2px;
        margin-bottom: 8px;
      }

      .sub-title {
        color: #666;
        font-size: 16px;
        margin-bottom: 15px;
      }

      .divider {
        width: 80px;
        height: 2px;
        background: #8bc38b;
        margin: 0 auto;
        border-radius: 1px;
      }
    }

    .form-section {
      margin-bottom: 30px;
      padding: 20px;
      background: #f9fcf9;
      border-radius: 10px;
      transition: all 0.3s;

      &:hover {
        box-shadow: 0 2px 12px rgba(139, 195, 139, 0.1);
      }

      .section-title {
        color: #5a9f5a;
        font-size: 16px;
        font-weight: 500;
        margin-bottom: 20px;
        padding-left: 10px;
        border-left: 4px solid #8bc38b;
      }
    }

    .form-item {
      margin-bottom: 20px;

      ::v-deep .el-form-item__label {
        color: #666;
        font-size: 14px;
        padding-bottom: 6px;
      }

      .custom-input {
        ::v-deep .el-input__inner {
          height: 42px;
          border-radius: 8px;
          border: 1px solid #d9e6d9;
          background: #fff;
          transition: all 0.3s;

          &:focus {
            border-color: #8bc38b;
            box-shadow: 0 0 8px rgba(139, 195, 139, 0.3);
          }

          &::placeholder {
            color: #bbb;
          }
        }
      }
    }

    .form-actions {
      text-align: center;
      margin-top: 30px;

      .register-btn {
        width: 200px;
        height: 44px;
        background: #6aaf6a;
        border: none;
        border-radius: 8px;
        font-size: 16px;
        letter-spacing: 2px;
        transition: all 0.3s;

        &:hover {
          background: #5a9f5a;
          transform: translateY(-2px);
          box-shadow: 0 5px 15px rgba(106, 175, 106, 0.3);
        }
      }

      .cancel-btn {
        width: 200px;
        height: 44px;
        background: #f0f9f0;
        border-color: #d9e6d9;
        color: #666;
        border-radius: 8px;
        margin-left: 20px;
        transition: all 0.3s;

        &:hover {
          background: #e1f3e1;
          color: #5a9f5a;
          transform: translateY(-2px);
        }
      }
    }
  }
}

@media (max-width: 768px) {
  .register-card {
    width: 90% !important;
    padding: 30px 20px !important;

    .form-actions {
      .register-btn,
      .cancel-btn {
        width: 100% !important;
        margin-left: 0 !important;
        margin-top: 10px;
      }
    }
  }
}
</style>